//引入包资源
const gulp = require("gulp");
const uglify = require("gulp-uglify");
const cleanCSS = require("gulp-clean-css");
const sass = require("gulp-sass")(require("sass"))
const html = require("gulp-htmlmin");
const htmlmin = require("gulp-htmlmin");
const del = require("del");
const connect = require("gulp-connect");
const paths = {
    js: {
        src: "src/js/**/*.js",
        dest: "dist/js"
    },
    css:{
        src: "src/css/**/*.css",
        dest: "dist/css"
    },
    scss:{
        src:"src/scss/**/*.scss",
        dest:"dist/css"
    },
    html:{
        src:"src/**/*.html",
        dest:"dist"
    },
    libs:{
        src:"src/libs/**/*.*",
        dest:"dist/libs"
    },
    images:{
        src:"src/images/**/*.*",
        dest:"dist/images"
    }
}
//定义任务函数
function scripts() {
    return gulp.src(paths.js.src)
        .pipe(uglify())
        .pipe(gulp.dest(paths.js.dest))
}
//处理css
function styles(){
    return gulp.src(paths.css.src)
    .pipe(cleanCSS())
    .pipe(gulp.dest(paths.css.dest))
}
//处理sass
function buildScss(){
    return gulp.src(paths.scss.src)
    .pipe(sass({outputStyle:"compressed"}))
    .pipe(gulp.dest(paths.scss.dest))
}
//处理html代码
function Html(){
    return gulp.src(paths.html.src)
    .pipe(htmlmin())
    .pipe(gulp.dest(paths.html.dest))
}
//拷贝libs
function copylibs(){
    return gulp.src(paths.libs.src)
    .pipe(gulp.dest(paths.libs.dest));

}
//拷贝图片
function copyImage(){
    return gulp.src(paths.images.src)
    .pipe(gulp.dest(paths.images.dest));

}
//定义监视任务
function watch(){
    gulp.watch(paths.scss.src,buildScss)
    gulp.watch(paths.js.src,scripts)
    gulp.watch(paths.css.src,styles)
    gulp.watch(paths.html.src,Html)
    gulp.watch(paths.libs.src,copylibs)
    gulp.watch(paths.images.src,copyImage)
}
//启动websever服务器
function server(){
    connect.server({
        root:"dist",
        port:9527,
        livereload:true
    })
}
//清理dest目录
function clean(){
    return del(["dist"])
}
 //定义流程化
 const build = gulp.series(clean,gulp.parallel(styles,scripts,buildScss,Html,copylibs,copyImage,server,watch))      
// 编译
//导处任务
exports.scripts = scripts;
exports.styles =styles;
exports.buildScss = buildScss; 
exports.Html = Html;
exports.watch = watch;
exports.clean = clean;
exports.copylibs = copylibs;
exports.copyImage = copyImage;
exports.server = server;
exports.build = build;
//默认执行的任务
exports.default = build;